import $api from '.';

interface Login {
  uname: string;
  password: string;
  strategy: string;
}

// 登录 uname password strategy:local
const authentication = async (data: Login) => {
  const res = await $api.post('/authentication', {
    ...data,
  });

  return res;
};

// 添加用户
const addAdmins = async (data: any) => {
  const res = await $api.post('/admins', {
    ...data,
  });

  return res;
};

// 修改用户
const editAdmins = async (id: string | number, data: any) => {
  const res = await $api.patch(`/admins/${id}`, {
    ...data,
  });
  return res;
};

// 删除
const delAdmins = async (id: string | number) => {
  const res = await $api.delete(`/admins/${id}`);
  return res;
};

// 查一个
const getAdmins = async (id: string | number) => {
  const res = await $api.get(`/admins/${id}`);
  return res;
};

// 获取列表
const getAdminsList = async ({ keyword, $limit = 10, $page = 1 }: any) => {
  const $skip = ($page - 1) * $limit;
  let queryStr = '';
  if (keyword) queryStr += `&name=${keyword}`;

  const res = await $api.get(`/admins?$limit=${$limit}&$skip=${$skip}${queryStr}`);
  return res;
};

export default {
  authentication,
  addAdmins,
  editAdmins,
  delAdmins,
  getAdmins,
  getAdminsList,
};
